use dataset, clear

ssc install mat2txt, replace

sum year

gen time = (year-r(min))/(r(max)-r(min))

orthpoly time, generate(time1 time2) degree(2)

levelsof year, local(levels)

matrix D = J(23,23,.)
matrix P = J(23,23,.)

local i = 0
foreach l of local levels {
  di `l'
  local ++i
  local j = 0
  foreach m of local levels {
    local ++j
    recode year (`l'=0) (`m'=1) (else=.), gen(group)
	if `l'>`m' {
      quietly: ksmirnov ft_own, by(group)
	  matrix D[`i',`j'] = r(D)
	  matrix P[`i',`j'] = r(p)
	}
    drop group
  }
}

mat2txt, matrix(D) saving("tableA3_d.txt") format(%3.2f) replace
mat2txt, matrix(P) saving("tableA3_p.txt") format(%4.3f) replace
